package com.binguo.api.controller;

import com.binguo.api.result.Result;
import com.binguo.api.form.InitForm;
import com.binguo.api.form.PhoneCodeForm;
import com.binguo.common.enums.CodeType;
import com.binguo.api.result.ResultGenerator;
import com.binguo.model.api.vo.InitInfoVO;
import com.binguo.model.api.vo.InitVO;
import com.binguo.service.InitService;
import com.binguo.service.SMSservice;
import com.binguo.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


/**
 * Created by haibang on 18/5/7.
 */
@Api(tags = "收银端: 初始化")
@RestController
@RequestMapping
public class InitController {

    private static Logger logger = LoggerFactory.getLogger(InitController.class);

    @Autowired
    SMSservice smSservice;

    @Autowired
    InitService initService;

    @Autowired
    ShopService shopService;

    @GetMapping(value = "/api/init/code", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取初始验证码", notes = "通过手机号,测试手机号18601763142")
    public Result getInitCode(@ApiParam("手机号") @RequestParam @NotNull String phone) {
        smSservice.sendInitCode(phone, CodeType.INITCODE);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping(value = "/api/init/phone/check", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "初始化手机号检查", notes = "通过短信验证码,测试手机号18601763142, 测试code任意")
    public Result checkInitCode(@RequestBody @Valid PhoneCodeForm form) {
        //fixme haibang 测试不checkCode
//        smSservice.checkInitCode(form.getPhone(), form.getCode(), CodeType.INITCODE);
        shopService.checkPhone(form.getPhone());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping(value = "/api/init", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "初始化", notes = "通过邀请码,手机号,测试邀请码xxxxxx(手机号或验证码不能都为空)")
    public Result checkInit(@RequestBody @Valid InitForm form) {
        form.validate();
        shopService.checkPhone(form.getPhone());
        InitVO vo = initService.init(form.getInviteCode(), form.getPhone(), form.getDeviceId(), form.getShopName(), form.getCashierDeskName());
        return ResultGenerator.genSuccessResult(vo);
    }


    @GetMapping(value = "/api/init/info/{deviceId}", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取初始化信息", notes = "通过deviceId")
    public Result getInitInfo(@PathVariable String deviceId) {
        InitInfoVO vo = initService.getInitInfo(deviceId);
        return ResultGenerator.genSuccessResult(vo);
    }






}
